home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / NmSpcUtl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  4.5 KB  |  210 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        NmSpcUtl.cpp
  3.  
  4.     Contains:    Utilities for working with NameSpaces.
  5.  
  6.     Owned by:    David McCusker
  7.  
  8.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #ifndef _NMSPCUTL_
  13. #include <NmSpcUtl.h>
  14. #endif
  15.  
  16. #ifndef SOM_ODNameSpaceManager_xh
  17. #include <NmSpcMg.xh>
  18. #endif
  19.  
  20. #ifndef SOM_ODValueNameSpace_xh
  21. #include <ValueNS.xh>
  22. #endif
  23.  
  24. #ifndef SOM_ODValueIterator_xh
  25. #include <ValueItr.xh>
  26. #endif
  27.  
  28. #ifndef SOM_ODSession_xh
  29. #include <ODSessn.xh>
  30. #endif
  31.  
  32. #ifndef _BARRAY_
  33. #include <BArray.h>
  34. #endif
  35.  
  36. #ifndef _MEMMGR_
  37. #include <MemMgr.h>
  38. #endif
  39.  
  40. #ifndef _ODMEMORY_
  41. #include "ODMemory.h"
  42. #endif
  43.  
  44. #ifndef _ITEXT_
  45. #include "IText.h"
  46. #endif
  47.  
  48.  
  49. void ValueNameSpaceRegister(ODValueNameSpace* ns, 
  50.                             Environment* ev,
  51.                             ODISOStr key,
  52.                             ODPtr buffer, 
  53.                             ODULong size)
  54. {
  55.     ODByteArray    ba;
  56.     ba._buffer = (octet*) buffer;
  57.     ba._length = size;
  58.     ba._maximum = size;
  59.     
  60.     ns->Register(ev, key, &ba);
  61. }
  62.  
  63. ODBoolean ValueNameSpaceGetEntry(ODValueNameSpace* ns, 
  64.                                  Environment* ev,
  65.                                    ODISOStr key,
  66.                                    ODPtr* value, 
  67.                                    ODULong* valueLength)
  68. {
  69.     ODByteArray ba;
  70.  
  71.     if (ns->GetEntry(ev, key, &ba))
  72.     {
  73.         *value = ba._buffer;
  74.         *valueLength = ba._length;
  75.         return kODTrue;
  76.     }
  77.     else
  78.     {
  79.         *value = kODNULL;
  80.         return kODFalse;
  81.     }
  82. }
  83.  
  84. ODBoolean ValueNameSpaceGetODName(ODValueNameSpace* ns, 
  85.                                  Environment* ev,
  86.                                    ODISOStr key,
  87.                                    ODName** value)
  88. {
  89.     ODByteArray ba;
  90.     
  91.     if (ns->GetEntry(ev, key, &ba))
  92.     {
  93.         ODTradITextData* theIName = (ODTradITextData*)ODNewPtrClear(ba._length, kDefaultHeapID);
  94.         ODBlockMove(ba._buffer, theIName, ba._length);
  95.         ODDisposePtr( ba._buffer );
  96.         ODULong textLen = ba._length - sizeof(ODTradITextDataHeader);
  97.         StringPtr thePString = (StringPtr)ODNewPtrClear(textLen+1);
  98.         thePString[0] = textLen;
  99.         ODBlockMove((ODPtr)theIName->theText, (ODPtr)&thePString[1], textLen );
  100.         *value = CreateITextPString(theIName->theScriptCode, 
  101.                              theIName->theLangCode, 
  102.                              thePString);
  103.         ODDisposePtr( theIName );
  104.         ODDisposePtr( thePString );
  105.         return kODTrue;
  106.     }
  107.     else
  108.     {
  109.         *value = kODNULL;
  110.         return kODFalse;
  111.     }
  112. }
  113.  
  114. void ValueIteratorFirst(ODValueIterator* iter, 
  115.                         Environment* ev,
  116.                         ODISOStr* key,
  117.                         ODPtr* value, 
  118.                         ODULong* valueLength)
  119. {
  120.     ODByteArray ba;
  121.     ba._length = 0;
  122.     
  123.     iter->First(ev, key, &ba);
  124.     
  125.     if (ba._length > 0)
  126.     {
  127.         *value = ba._buffer;
  128.         *valueLength = ba._length;
  129.     }
  130.     else
  131.         *value = kODNULL;
  132. }
  133.  
  134. void ValueIteratorNext(ODValueIterator* iter, 
  135.                         Environment* ev,
  136.                         ODISOStr* key,
  137.                         ODPtr* value, 
  138.                         ODULong* valueLength)
  139. {
  140.     ODByteArray ba;
  141.     ba._length = 0;
  142.     
  143.     iter->Next(ev, key, &ba);
  144.     
  145.     if (ba._length > 0)
  146.     {
  147.         *value = ba._buffer;
  148.         *valueLength = ba._length;
  149.     }
  150.     else
  151.         *value = kODNULL;
  152. }
  153.  
  154.  
  155. ODISOStr ValueNameSpaceGetString( ODSession* sessn, ODISOStr nameSpaceName, ODISOStr key )
  156. {
  157.     ODByteArray ba;
  158.     ODISOStr result = kODNULL;
  159.     Environment* ev = somGetGlobalEnvironment();
  160.     ODNameSpaceManager* mgr = sessn->GetNameSpaceManager(ev);
  161.     ODValueNameSpace* ns = (ODValueNameSpace*) mgr->HasNameSpace(ev, nameSpaceName);
  162.     if ( ns && ns->GetEntry(ev, key, &ba) && ba._length > 0 )
  163.         result = (ODISOStr) ba._buffer;
  164.  
  165.     // The old version of this function used to allocate space for a terminating
  166.     // null byte; but as near as I can tell, the only ways that strings become
  167.     // values also allocate the null byte space (as they should) when the entry
  168.     // is orginally created.
  169.     
  170.     return result;
  171. }
  172.  
  173. void ValueNameSpaceWriteToFile( ODValueNameSpace* ns, PlatformFile* file )
  174. {
  175.     Environment* ev = somGetGlobalEnvironment();
  176.     ODByteArray ba; 
  177.     ba._buffer = (octet*) file; 
  178.     ba._length = sizeof(PlatformFile); 
  179.     ns->WriteToFile( ev, &ba );
  180. }
  181.  
  182. void ValueNameSpaceReadFromFile( ODValueNameSpace* ns, PlatformFile* file )
  183. {
  184.     Environment* ev = somGetGlobalEnvironment();
  185.     ODByteArray ba; 
  186.     ba._buffer = (octet*) file; 
  187.     ba._length = sizeof(PlatformFile); 
  188.     ns->ReadFromFile( ev, &ba );
  189. }
  190.  
  191.  
  192. //------------------------------------------------------------------------------
  193. // CreateFlatIText
  194. //------------------------------------------------------------------------------
  195.  
  196. ODTradITextData* CreateFlatIText(ODScriptCode theScriptCode, ODLangCode theLangCode, 
  197.                                                 char* theText, ODUShort textLen)
  198. {
  199.     ODTradITextData* result = (ODTradITextData*)
  200.              ODNewPtrClear(textLen+sizeof(ODTradITextDataHeader), kDefaultHeapID);
  201.     result->theScriptCode = theScriptCode;
  202.     result->theLangCode = theLangCode;
  203.     
  204.     ODBlockMove((ODPtr)theText, (ODPtr)result->theText, textLen);
  205.     
  206.     return result;
  207. }
  208.  
  209.  
  210.